package com.jokesoft.supermanager.common.save;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.jokesoft.supermanager.common.services.Address;
import com.jokesoft.supermanager.common.services.PersonServiceRequest;

public class SavePersonDataRequest extends PersonServiceRequest {

    private List<Address> addresses;

    @JsonCreator
    public SavePersonDataRequest(@JsonProperty("firstName") String firstName,
            @JsonProperty("lastName") String lastName, @JsonProperty("role") String role,
            @JsonProperty("addresses") List<String> addresses) throws AddressParseException {
        super(firstName, lastName, role);

        List<Address> newAddressList = new ArrayList<Address>();

        if (addresses != null) {
            for (String address : addresses) {
                String[] addcomp = address.split(",");
                if (addcomp.length == 4) {
                    Address parsedAddress = new Address(addcomp[0], addcomp[1], addcomp[2], addcomp[3]);
                    newAddressList.add(parsedAddress);
                } else {
                    throw new AddressParseException(addcomp);
                }
            }
            this.addresses = newAddressList;
        }

    }

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    @Override
    public String toString() {
        return "SavePersonDataRequest [addresses=" + addresses + ", getFirstName()=" + getFirstName()
                + ", getLastName()=" + getLastName() + ", getRole()=" + getRole() + ", hashCode()=" + hashCode()
                + ", getClass()=" + getClass() + ", toString()=" + super.toString() + "]";
    }

}
